layui.use(['table','layer','laydate','layedit','form'], function() {
	var table = layui.table;
	var table2 = layui.table;
	var laydate = layui.laydate;
	var layedit = layui.layedit;
	var form = layui.form;
	var $ = layui.jquery, layer = layui.layer;
	
	/** 动态加载阶段下拉框 **/
	$.ajax({
		type : 'get',
		url : '../json/schoolType.json',
		datatype : 'json',
		success : function(schooltypedate) {
			if (schooltypedate.code == 0) {
				var str = "";
				for(var i = 0; i < schooltypedate.count; i++){
					str += '<option value=' + schooltypedate.data[i].name + '>'+ schooltypedate.data[i].name+'</option>';
				}
				$("#stage").append(str);
				form.render("select");
			}
			else{
				layer.msg('阶段信息获取失败', {icon: 2});
			}
		}
	});
	
	form.on('select(stage)', function(data){
		//layer.msg(data.value, {icon: 1});
		$.ajax({
			type : 'get',
			url : '../sysschoolmanagement/getschoolbywhere?schooltype=' + data.value,
			datatype : 'json',
			success : function(schoolData) {
				if (schoolData.code == 0) {
					var str = "";
					for(var i = 0; i < schoolData.count; i++){
						str += '<option value=' + schoolData.data[i].schoolid + '>'+ schoolData.data[i].schoolName+'</option>';
					}
					$("#school").html("");
					$("#school").append(str);
					form.render("select");
				}
				else{
					$("#school").html("");
					form.render("select");
					layer.msg('学校信息获取失败', {icon: 2});
				}
			}
		});
		$.ajax({
			type : 'get',
			url : '../json/grade.json',
			datatype : 'json',
			success : function(gradedate) {
				if (gradedate.code == 0) {
					var str = "";
					for(var i = 0; i < gradedate.count; i++){
						if(data.value == 0){
							str += '<option value=' + gradedate.data[i].gradename + '>'+ gradedate.data[i].gradename+'</option>';
						}
						else{
							if(gradedate.data[i].schoolType == data.value){
								str += '<option value=' + gradedate.data[i].gradename + '>'+ gradedate.data[i].gradename+'</option>';
							}	
						}						
					}
					$("#gradename").html("");
					$("#gradename").append(str);
					form.render("select");
				}
				else{
					$("#gradename").html("");
					form.render("select");
					layer.msg('年级信息获取失败', {icon: 2});
				}
			}
		});
	});
	
	/*动态加载角色下拉框*/
	function loadDownBox(values){
		if(values != ''){
			$.ajax({
				type : 'get',
				url : '../systemmanagement/rolemanagementgetrolelist?addRoleid=18',
				datatype : 'json',
				success : function(roledata) {
					if (roledata.code == 0) {
						var str = "";
						for(var i = 0; i < roledata.count; i++){
							if(roledata.data[i].id == values){
								str += '<option value=' + roledata.data[i].id + ' selected >'+ roledata.data[i].name +'</option>';
							}
							else{
								str += '<option value=' + roledata.data[i].id + '>'+ roledata.data[i].name +'</option>';
							}							
						}
						$("#userType").append(str);
						form.render();
					}
					else{
						layer.msg("角色信息获取失败");
					}
				}
			});
		}
		else{
			$.ajax({
				type : 'get',
				url : '../systemmanagement/rolemanagementgetrolelist?addRoleid=18',
				datatype : 'json',
				success : function(roledata) {
					if (roledata.code == 0) {
						var str = "";
						for(var i = 0; i < roledata.count; i++){
							str += '<option value=' + roledata.data[i].id + '>'+ roledata.data[i].name +'</option>';
						}
						$("#userType").append(str);
						form.render();
					}
					else{
						layer.msg("角色信息获取失败");
					}
				}
			});
		}
		
	}
	
	loadDownBox('');
	
	$("#btnselteac").click(function(){
		var idorname=$("#userName").val().trim();
		var roleid = $("#userType").val();
		table.render({
			elem: '#teacherData',
			url: '../systeachermanagement/getteacherbypage?userName=' + idorname + "&roleid=" + roleid,
			title: '教师数据表',
			height:200,
			skin: 'line',
			even: true,
			cols: [
				[{
					field: 'userName',
					title: '账号',
					align: "center",
					width:'10%'
				}, {
					field: 'realName',
					title: '真实姓名',
					align: "center",
					width:'20%'
				}, {
					field: 'sex',
					title: '性别',
					align: "center",
					width:'10%'
				}, {
					field: 'email',
					title: '邮箱',
					align: "center",
					width:'24%'
				}, {
					field: 'phone',
					title: '联系电话',
					align: "center",
					width:'24%'
				}, {
					title: '操作',
					toolbar: '#barDemo',
					align: "center",
					width:'10%'
				}]
			]
		});
		
	});
    
    /*查看详情事件*/
    table.on('tool(teacherData)', function(obj){
	    var data = obj.data;
	    if(obj.event === 'btnselstuarc'){
	    	$(".teaCourseSet-con .astuarc").css("display","block");
	    	$("#tearoleid").val(data.roleid);
	    	$("#teateaid").val(data.teainfoid);
	    	$("#teaUserName").val(data.userName);
	    	$("#tearelName").val(data.realName);
	    	
	    	var userName=data.userName;
	    	//单个教师班级设置详情
	        table2.render({
	    		elem: '#ateaCourse',
	    		id:'userName',
	    		url: '../systeachermanagement/getteacherrole?userName='+userName,
	    		toolbar: '#toolbarDemo1',
	    		title: '单个教师班级设置详情',
	    		skin:'line',
	    		even:'true',
	    		cols: [
						[{
							field: 'userName',
							align: 'center',
							title: '教师账号'
						},{
							field: 'realName',
							align: 'center',
							title: '教师姓名'							
						},{
							field: 'sex',
							align: 'center',
							title: '性别'							
						}, {
							field: 'gradename',
							align: 'center',
							title: '所带年级'						
						}, {
							field: 'className',
							align: 'center',
							title: '所带班级',
						}, {
							field: 'post',
							align: 'center',
							title: '职务'							
						},{
							field: 'subName',
							align: 'center',
							title: '科目'							
						},{
							title: '操作',
							toolbar: '#delTea',
							align: 'center'
						}]
				],
	    	});
	    }
    });
	
	//添加教师班级设置
	table2.on('toolbar(ateaCourse)', function(obj){
	    var data = obj.data;
	    switch(obj.event) {
			case 'addBtn':
				
			/*动态加载年级下拉框*/
			$.ajax({
				type : 'get',
				url : '../json/grade.json',
				datatype : 'json',
				success : function(gradedate) {
					if (gradedate.code == 0) {
						var str = "";
						for(var i = 0; i < gradedate.count; i++){
							str += '<option value=' + gradedate.data[i].gradename + '>'+ gradedate.data[i].gradename+'</option>';
						}
						$("#gradename").append(str);
						form.render("select");
					}
					else{
						layer.msg("年级信息获取失败");
					}
				}
			});
			
			/*点击年级下拉框记载班级*/
			form.on('select(gradename)', function(data) {
				var grade=data.value;
				/*动态加载班级下拉框*/
				$("#classid").html("");
				$.ajax({
					type : 'get',
					url : '../sysclassmanagement/getclassbygrade?grade='+grade,
					datatype : 'json',
					success : function(classdate) {
						if (classdate.code == 0) {
							var str = "";
							for(var i = 0; i < classdate.count; i++){
								str += '<option value=' + classdate.data[i].classid + '>'+ classdate.data[i].className +'</option>';
							}
							$("#classid").append(str);
							form.render("select");
						}
						else{
							$("#classid").html("");
							layer.msg("班级信息获取失败");
							form.render("select");
							
						}
					}
				});
			}); 
			
			/*加载教师职务*/
			$.ajax({
				type : 'get',
				url : '../systeachermanagement/getcloudsubject',
				datatype : 'json',
				success : function(closubject) {
					if (closubject.code == 10001) {
						var str = "";
						for(var i = 0; i < closubject.count; i++){
							str += '<option value=' + closubject.data[i].subid + '>'+ closubject.data[i].subName+'</option>';
						}
						$("#selsubject").append(str);
						form.render("select");
					}
					else{
						layer.msg("职务信息获取失败");
					}
				}
			});
			
			//弹出即全屏
	    	var index = layer.open({
		        type: 1, 
		        title: '添加教师角色',
		        area: ['600px', '500px'],
		        maxmin: true,
		        shade: 0.8,
		        content: $('#addTeaCoure'),
		        cancel: function(){ 
				   $(".addTeaCoure-con").css("display","none");
				}
		    });
		    layer.full(index);
		};
    });
	
	$("#saveTeaCoure").click(function(){
		var roleid=$("#tearoleid").val();
    	var teaid=$("#teateaid").val();
		var classid=$("#classid").val();
		var subid=$("#selsubject").val();
		$.ajax({
			type : 'get',
			url : '../systeachermanagement/addteacherrole?roleid='+roleid+"&teaid="+teaid+"&classid="+classid+"&subid="+subid,
			datatype : 'json',
			success : function(date) {
				if (date.code == 10001) {
					layer.msg("教师班级设置成功！");
					layer.close();
					table2.reload("userName", { //此处是上文提到的 初始化标识id
		                where: {
		                	keyword:data.code=='10001'
		                }
		            });	
				}
				else{
					layer.close();
					layer.msg("教师班级设置失败！");
				}
			}
		});
	});
	
	table.on('tool(ateaCourse)', function(obj){
	    var data = obj.data;
	    var teaclass=data.teachid;
	    if(obj.event === 'delBtn'){
	    	layer.confirm('你确定要删除该教师任课吗？', {
	    		  btn: ['确定','取消']
	    		}, function(){
	    			$.ajax({
	    				type : 'get',
	    				url : '../systeachermanagement/deltearole?teachid='+teaclass,
	    				datatype : 'json',
	    				success : function(date) {
	    					if (date.code == 10001) {
	    						layer.msg("教师任课删除成功！");
	    						table2.reload("userName", { //此处是上文提到的 初始化标识id
	    			                where: {
	    			                	keyword:data.code=='10001'
	    			                }
	    			            });	
	    					}
	    					else{
	    						layer.msg("教师任课删除失败！");
	    					}
	    				}
	    			});
	    		}, function(){
	    			layer.msg("取消成功！");
	    		}
	    	);
	    }
	 
	});
	
});